<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新增</title>
    <link rel="stylesheet" href="./css/reset.css">
    <style>
        body {
            background-color: gainsboro;
        }

        #app {
            width: 320px;
            height: 400px;
            background-color: #fff;
            margin: 100px auto;
            padding: 10px;
        }

        .input_box {
            height: 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            /* background-color: red; */
        }

        .input_box input {
            width: 240px;
            height: 30px;
            background-color: gainsboro;
            padding: 0 10px;
        }

        .add {
            color: rgb(11, 194, 194);
            cursor: pointer;
            margin-right: 10px;
        }

        .opts {
            height: 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .opts span {
            background-color: rgb(11, 194, 194);
            padding: 5px;
            border-radius: 5px;
            color: #fff;
        }

        .opts .unfinished {
            background-color: red;
        }

        p {
            line-height: 40px;
            font-weight: 700;
        }

        .list {
            height: 270px;
            overflow-y: auto;
            /* background-color: aquamarine; */
        }

        .item {
            height: 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: red;
            color: #fff;
            margin: 10px 0;
        }

        .finished {
            background-color: rgb(11, 194, 194);
        }

        .item .id,
        .checkbox,
        .name {
            margin: 0 10px;
        }

        .name {
            flex: 1;
        }

        .remove {
            cursor: pointer;
        }

        .empty {
            text-align: center;
            line-height: 40px;
            font-size: 14px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="input_box">
            <input type="text" placeholder="输入任务名称" v-model="inputText">
            <span class="add" @click="add">新增</span>
        </div>
        <div class="opts">
            <span>全部任务( {{tasklist.length}} )</span>
            <span>已完成( {{finishedCount}} )</span>
            <span class="unfinished">未完成( {{unfinishedCount}} )</span>
        </div>
        <p>任务列表</p>
        <div class="list">
            <div :class="{item:true,finished:item.status}" v-for="(item,index) in tasklist" :key="index">
                <input class="checkbox" type="checkbox" @click="changeStatus(item)">
                <div class="id">{{item.id}}</div>
                <div class="name">{{item.name}}</div>
                <div class="remove" @click="remove(item)">删除</div>
            </div>
            <div class="empty" v-show="tasklist.length == 0">
                任务列表为空，请添加一个任务！
            </div>
        </div>
    </div>
</body>

</html>
<script src="./js/vue-2.5.21.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: {
            inputText: '',
            tasklist: [],
            finishedCount: 0,
            unfinishedCount: 0,
        },
        methods: {
            add() {
                if (this.inputText) {
                    this.tasklist.push({ id: this.tasklist.length + 1, name: this.inputText, status: false })
                    this.inputText = ''
                }
            },
            changeStatus(item) {
                item.status = !item.status
            },
            remove(item) {
                let i = this.tasklist.findIndex((it) => {
                    return it.id == item.id
                })
                this.tasklist.splice(i, 1)
            }
        },
        watch: {
            tasklist: {
                handler() {
                    this.finishedCount = this.tasklist.filter((item) => {
                        return item.status
                    }).length
                    this.unfinishedCount = this.tasklist.filter((item) => {
                        return !item.status
                    }).length
                },
                deep: true
            }
        }
    })
</script>